<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="专家姓名" prop="expertName">
        <el-input
          v-model="queryParams.expertName"
          placeholder="请输入专家姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件类型" prop="identityTypeId">
        <el-select v-model="queryParams.identityTypeId" clearable placeholder="请选择">
            <el-option
              v-for="item in identitytypeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
      </el-form-item>
      </el-form-item>

      <el-form-item label="地区" prop="areaId">
        <el-select v-model="queryParams.areaId" clearable placeholder="请选择">
            <el-option
              v-for="item in companyareaList"
              :key="item.areaId"
              :label="item.areaName"
              :value="item.areaId">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="专家类型" prop="expertTypeId">
        <el-select v-model="queryParams.expertTypeId" clearable placeholder="请选择">
            <el-option
              v-for="item in experttypeList"
              :key="item.id"
              :label="item.text"
              :value="item.id">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:expert:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:expert:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:expert:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:expert:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="expertList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="专家姓名" align="center" prop="expertName" />
      <el-table-column label="手机号码" align="center" prop="mobile" />
      <el-table-column label="证件类型" align="center" prop="identityTypeName" />
      <el-table-column label="证件号码" align="center" prop="identityCode" />
      <el-table-column label="地区" align="center" prop="areaName" />
      <el-table-column label="专家类型" align="center" prop="expertTypeName" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="性别" prop="sex"  width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex === 1">男</el-tag>
          <el-tag v-if="scope.row.sex === 2" type="success">女</el-tag>
          <el-tag v-if="scope.row.sex === 0" type="info">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="专家品目" align="center" prop="expertCategoryName" />
      <el-table-column label="是否在职" align="center" prop="isOnJob" />
      <el-table-column label="最高学历" align="center" prop="highestEducation" />
      <el-table-column label="职务" align="center" prop="postition" />
      <el-table-column label="状态" prop="status"  width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status">正常</el-tag>
          <el-tag v-if="!scope.row.status" type="info">停用</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="审批状态" align="center" prop="approvalStatus" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:expert:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:expert:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改专家管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="专家姓名" prop="expertName">
          <el-input v-model="form.expertName" placeholder="请输入专家姓名" />
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="证件类型" prop="identityTypeId">
          <el-select v-model="form.identityTypeId" @change="selectIdentityType" clearable placeholder="请选择">
              <el-option
                v-for="item in identitytypeList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="证件号码" prop="identityCode">
          <el-input v-model="form.identityCode" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item label="地区" prop="areaId">
          <el-select v-model="form.areaId" @change="selectArea" clearable placeholder="请选择">
              <el-option
                v-for="item in companyareaList"
                :key="item.areaId"
                :label="item.areaName"
                :value="item.areaId">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="专家类型" prop="expertTypeId">
          <el-select v-model="form.expertTypeId" @change="selectExpertType" clearable placeholder="请选择">
              <el-option
                v-for="item in experttypeList"
                :key="item.id"
                :label="item.text"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="专家品目" prop="expertCategoryId">
          <el-select v-model="form.expertCategoryId" multiple @change="selectExpertCategory" clearable placeholder="请选择">
              <el-option
                v-for="item in expertcategoryList"
                :key="item.id"
                :label="item.categoryName"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="最高学历" prop="highestEducation">
          <el-input v-model="form.highestEducation" placeholder="请输入最高学历" />
        </el-form-item>
        <el-form-item label="专家简介" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExpert, getExpert, delExpert, addExpert, updateExpert } from "@/api/system/expert";
import { listsIdentitytype } from "@/api/system/identitytype";
import { listsCompanyarea } from "@/api/system/companyarea";
import { listsExpertcategory } from "@/api/system/expertcategory";
import { listsType } from "@/api/system/experttype";
import { listsCompanytype } from "@/api/system/companytype";

export default {
  name: "Expert",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专家管理表格数据
      expertList: [],
      // 地区列表
      companyareaList: [],
      // 身份类型列表
      identitytypeList: [],
      // 专家品目列表
      expertcategoryList: [],
      // 专家类型列表
      experttypeList: [],
      // 企业类型列表
      companytypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        expertName: null,
        mobile: null,
        identityTypeId: null,
        identityTypeName: null,
        identityCode: null,
        areaId: null,
        areaName: null,
        expertTypeId: null,
        expertTypeName: null,
        sex: null,
        email: null,
        expertCategoryId: null,
        expertCategoryName: null,
        isOnJob: null,
        workCompany: null,
        graduatedFrom: null,
        highestEducation: null,
        technicaTitle: null,
        postition: null,
        addr: null,
        isJoinExpertDb: null,
        status: null,
        bankName: null,
        bankAccount: null,
        attachmentOtherImage: null,
        attachmentImage: null,
        professionalTypeSelect: null,
        categoryCodeRemark: null,
        workingYears: null,
        userName: null,
        password: null,
        pwdPlaintext: null,
        approvalStatus: null,
        approvalRemark: null,
        fromType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getCompanyAreaList();
    this.getIdentitytypeList();
    this.getExpertCategoryList();
    this.getExpertTypeList();
  },
  methods: {
    /** 查询专家管理列表 */
    getList() {
      this.loading = true;
      listExpert(this.queryParams).then(response => {
        this.expertList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        expertName: null,
        mobile: null,
        identityTypeId: null,
        identityTypeName: null,
        identityCode: null,
        areaId: null,
        areaName: null,
        expertTypeId: null,
        expertTypeName: null,
        sex: null,
        email: null,
        expertCategoryId: null,
        expertCategoryName: null,
        isOnJob: null,
        workCompany: null,
        graduatedFrom: null,
        highestEducation: null,
        technicaTitle: null,
        postition: null,
        addr: null,
        remark: null,
        isJoinExpertDb: null,
        status: null,
        bankName: null,
        bankAccount: null,
        attachmentOtherImage: null,
        attachmentImage: null,
        professionalTypeSelect: null,
        categoryCodeRemark: null,
        workingYears: null,
        userName: null,
        password: null,
        pwdPlaintext: null,
        approvalStatus: null,
        approvalRemark: null,
        fromType: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加专家管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getExpert(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改专家管理";
        this.form.expertCategoryId = this.form.expertCategoryId.split(',');
        console.log(this.form.expertCategoryId)
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.expertCategoryId= this.form.expertCategoryId.join(',');
          if (this.form.id != null) {
            updateExpert(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExpert(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除专家管理编号为"' + ids + '"的数据项？').then(function() {
        return delExpert(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/expert/export', {
        ...this.queryParams
      }, `expert_${new Date().getTime()}.xlsx`)
    },
    /**获取地区列表**/
    getCompanyAreaList(){
      this.loading = true;
      listsCompanyarea(this.queryParams).then(response => {
        this.companyareaList = response.data;
        this.loading = false;
      });
    },
    /**获取身份类型列表**/
    getIdentitytypeList(){
      this.loading = true;
      listsIdentitytype(this.queryParams).then(response => {
        this.identitytypeList = response.data;
        this.loading = false;
      });
    },
    /**获取专家品目列表**/
    getExpertCategoryList(){
      this.loading = true;
      listsExpertcategory(this.queryParams).then(response => {
        this.expertcategoryList = response.data;
        this.loading = false;
      });
    },
    /**获取专家型列表**/
    getExpertTypeList(){
      this.loading = true;
      listsType(this.queryParams).then(response => {
        this.experttypeList = response.data;
        this.loading = false;
      });
    },
    selectIdentityType(value) {
      // 通过已选的value值找到对应的对象
      const selectedOption = this.identitytypeList.find(option => option.id === value);
        if (selectedOption) {
          this.form.identityTypeName = selectedOption.name;
        }
    },
    selectArea(value) {
      // 通过已选的value值找到对应的对象
      const selectedOption = this.companyareaList.find(option => option.areaId === value);
        if (selectedOption) {
          this.form.areaName = selectedOption.areaName;
        }
    },
    selectExpertType(value) {
      // 通过已选的value值找到对应的对象
      const selectedOption = this.experttypeList.find(option => option.id === value);
        if (selectedOption) {
          this.form.expertTypeName = selectedOption.text;
        }
    },
    selectExpertCategory(value) {
      // 通过已选的value值找到对应的对象
     // 当选中项变化时，获取对应的其他属性值
      const selectedOptions = this.expertcategoryList.filter(option =>
        value.includes(option.id)
      );
      const selectedProps = selectedOptions.map(option => option.categoryName);
      this.form.expertCategoryName = selectedProps.join(',');
    },


  }
};
</script>
